package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.common.response.JsonResult;
import cn.tedu._05weibo.common.response.StatusCode;
import cn.tedu._05weibo.mapper.WeiboMapper;
import cn.tedu._05weibo.pojo.dto.WeiboDTO;
import cn.tedu._05weibo.pojo.entity.Weibo;
import cn.tedu._05weibo.pojo.vo.UserVO;
import cn.tedu._05weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._05weibo.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/weibo/")
@Slf4j
@Api(tags = "02.微博模块")
@Validated
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    /**
     * -1- 发布微博功能
     * 接口地址: /v1/weibo/insert
     * 请求方式: POST
     * 请求数据类型: application/json
     * 响应数据类型: int
     */
    @ApiOperation("发布微博")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO , @ApiIgnore HttpSession session){
        log.debug("weiboDTO = " + weiboDTO );
        /*
            1.获取用户DTO信息
            2.校验用户登陆状态
                2.1 若已登录 insert -> 返回1
                2.2 若未登录 返回2
         */
        // 校验登陆状态
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null) {// 未登录
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        // 插入微博
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO ,weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());

        weiboMapper.insertWeibo(weibo);

        return JsonResult.ok();
    }
    /**
     * -2- 首页微博列表功能
     * 接口地址: /v1/weibo/selectIndex
     * 请求方式: GET
     * 请求数据类型: application/x-www-form-urlencoded
     * 响应数据类型: List<WeiboIndexVO>
     */
    @ApiOperation("首页微博列表")
    @GetMapping("selectIndex")
    public JsonResult selectIndex(){
        List<WeiboIndexVO> WeiboIndexVOS = weiboMapper.selectIndex();
        return JsonResult.ok(WeiboIndexVOS);
    }
    /**
     * -3- 微博详情功能
     * 接口地址: /v1/weibo/selectById
     * 请求方式: GET
     * 请求数据类型: application/x-www-form-urlencoded
     * 响应数据类型: WeiboDetailVO
     */
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "微博编号",required = true,dataType = "long"),
            @ApiImplicitParam(name = "username",value = "用户名",required = true,dataType = "string")
    })
    @ApiOperation("微博详情")
    @GetMapping("selectById")
    public JsonResult selectById(@Range(min = 200,max = 300,message = "ID值必须在200~300之间") @RequestParam Long id, String username){
        // 人为制造异常
        if (id < 0) {
            throw new IllegalArgumentException("ID值不能小于0");
        }

        //username参数仅用于演示Knife4j的注解 没有其他作用
        WeiboDetailVO weiboDetailVO = weiboMapper.selectById(id);
        return JsonResult.ok(weiboDetailVO);
    }
}
